package com.lxs.legou.order.client;

import com.lxs.legou.item.api.SkuApi;
import com.lxs.legou.item.po.Sku;
import feign.hystrix.FallbackFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestMapping;
import java.util.List;

/**
 * @Auther: Mcb
 * @Date: 2022/1/9 18:40
 * @Description: 使用fallbackFactory追踪熔断错误信息
 */
@FeignClient(name = "item-service",fallbackFactory = SkuClient.SkuClientFallbackFactory.class)
public interface SkuClient extends SkuApi {


    class SkuClientFallbackFactory implements FallbackFactory<SkuClient>{

        Logger logger = LoggerFactory.getLogger(SkuClientFallbackFactory.class);

        //fallbackFactory的返回值为接口方法降级实现的内部类
        @Override
        public SkuClient create(Throwable throwable) {

            throwable.printStackTrace();
            logger.error(throwable.getMessage());

            return new SkuClient() {
                @Override
                public List<Sku> selectSkusBySpuId(Long spuId) {
                    logger.error("fallbackFactory--SkuClient.selectSkusBySpuId");
                    return null;
                }

                @Override
                public Sku edit(Long id) {
                    logger.error("fallbackFactory--SkuClient.edit");
                    return null;
                }

                @Override
                public void decrCount(Long num, Long skuId) {
                    logger.error("fallbackFactory--SkuClient.decrCount");

                }

                @Override
                public void incrCount(Long num, Long skuId) {
                    logger.error("fallbackFactory--SkuClient.increaseCount");
                }
            };
        }
    }

    //已用fallbackFactory，不能再使用fallback
    @Component
    @RequestMapping("/order-sku-fallback")
    class SkuClientFallback implements SkuClient{

        @Override
        public List<Sku> selectSkusBySpuId(Long spuId) {
            return null;
        }

        @Override
        public Sku edit(Long id) {
            return null;
        }

        @Override
        public void decrCount(Long num, Long skuId) {
        }

        @Override
        public void incrCount(Long num, Long skuId) {
        }
    }
}
